"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】CREATE/DROP CONVERSION命令触发事件触发器
Create At   : 2024/10/15
Owner       : sungang14
Description :
    预置条件：
        1.创建事件触发器：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0008()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0008();
        CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0008();
        CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0008();
    测试步骤：
        1、创建CONVERSION:CREATE CONVERSION myconv FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8;
        2、删除CONVERSION:DROP CONVERSION myconv;
Expect      : 
    1、触发执行函数f_eventtrigger_0008()
     test_event_trigger: ddl_command_start,CREATE CONVERSION
    2、触发执行函数f_eventtrigger_0008()
     test_event_trigger: ddl_command_start,DROP CONVERSION
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.fun_name = "f_eventtrigger_0008()"
        self.conv_name = "u_eventtrigger_0008"
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.comsh.execut_db_sql(f'drop function if exists {self.fun_name};')
        self.comsh.execut_db_sql(f'drop CONVERSION if exists {self.conv_name};')
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION {self.fun_name} RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)

    def test_Index_file_damaged(self):
        self.logger.info("创建CONVERSION:CREATE CONVERSION conv_name FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8;")
        msg = self.comsh.execut_db_sql(
            f"CREATE CONVERSION {self.conv_name} FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8;")
        self.logger.info(msg)
        self.assertIn(f'CREATE CONVERSION', msg)
        self.assertIn(f'test_event_trigger: ddl_command_start CREATE CONVERSION', msg)
        self.assertNotIn(f'test_event_trigger: ddl_command_end CREATE CONVERSION', msg)

        self.logger.info('删除CONVERSION:DROP CONVERSION conv_name;')
        msg = self.comsh.execut_db_sql(f'DROP CONVERSION {self.conv_name};')
        self.logger.info(msg)
        self.assertIn(f'DROP CONVERSION', msg)
        self.assertIn(f'test_event_trigger: ddl_command_start DROP CONVERSION', msg)
        self.assertNotIn(f'test_event_trigger: ddl_command_end DROP CONVERSION', msg)
        self.assertNotIn(f'test_event_trigger: sql_drop DROP CONVERSION', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql(f"drop function if exists {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
